package xyz.csmcool.imuser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import xyz.csmcool.imuser.entity.About;
import xyz.csmcool.imuser.service.IAboutService;
import xyz.csmcool.imutil.utils.JwtUtils;
import xyz.csmcool.imutil.utils.ResultTool;
import xyz.csmcool.imutil.webVo.MyPage;
import xyz.csmcool.imutil.webVo.RedisService;
import xyz.csmcool.imutil.webVo.ResultCode;
import xyz.csmcool.imutil.webVo.ResultVo;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csmcool
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/about")
@RequiredArgsConstructor
public class AboutController {

    final IAboutService iAboutService;
    final RedisService redisService;

    @GetMapping("/get-about")
    public ResultVo getAbout() {
        About about = iAboutService.selectLastOne();
        return ResultTool.success(about);
    }

    @PostMapping("/admin/about")
    public ResultVo setAbout(@Validated @RequestBody About about) {
        boolean save = iAboutService.save(about);
        return save ? ResultTool.success() : ResultTool.fail();
    }

    @DeleteMapping("/admin/about/{id}")
    public ResultVo setAbout(@PathVariable("id") String id) {
        boolean save = iAboutService.removeById(id);
        return save ? ResultTool.success() : ResultTool.fail();
    }

    @GetMapping("/admin/{limit}/{page}")
    public ResultVo setAbout(@PathVariable Long limit, @PathVariable Long page, HttpServletRequest request) {
        if (limit < 0 || page < 0 ) {
            return ResultTool.fail(ResultCode.CUSTOM_FAIL.setCode(998).setMessage("页码或者页面大小不正确"));
        }
        Page<About> pageParam = new Page<About>(page,limit);
        QueryWrapper<About> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<About> aboutPage = iAboutService.page(pageParam, queryWrapper);
        MyPage<About> myPage = new MyPage<>(aboutPage);
        return ResultTool.success(myPage);
    }
}
